﻿using MediatR;
using SheepCultivation.Common.Domain.Week2;
using SheepCultivation.Common.Dto;
using SheepCultivation.Common.Infrastructure.Interface;
using SheepCultivation.Enum;

namespace SheepCultivation.API.AddliCation.BreedingD
{
    public class SheepvarietyCreateHandler : IRequestHandler<SheepvarietyCreateCommon, Result>
    {
        #region 仓储
        /// <summary>
        /// 羊只称重仓储
        /// </summary>
        public readonly IBaseRepository<SheepWeighed> SheepWeighedRepository;
        /// <summary>
        /// 品种仓储
        /// </summary>
        public readonly IBaseRepository<Sheepvariety> SheepvarietyRepository;
        /// <summary>
        /// 日志
        /// </summary>
        public readonly ILogger<SheepvarietyCreateHandler> logger;
        public SheepvarietyCreateHandler(IBaseRepository<SheepWeighed> sheepWeighedRepository, IBaseRepository<Sheepvariety> sheepvarietyRepository, ILogger<SheepvarietyCreateHandler> logger)
        {
            SheepWeighedRepository = sheepWeighedRepository;
            SheepvarietyRepository = sheepvarietyRepository;
            this.logger = logger;
        }
        #endregion

        #region 品种新增
        /// <summary>
        /// 品种新增
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<Result> Handle(SheepvarietyCreateCommon request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonEnumCode.error };
            //异常处理
            try
            {
                logger.LogInformation("品种新增成功!");
                var res = new Sheepvariety()
                {
                    SheepvarietyName = request.SheepvarietyName,
                };
                SheepvarietyRepository.Add(res);

                result.Code = (int)CommonEnumCode.success;
                result.Msg = "品种新增成功";

            }
            catch (Exception ex)
            {
                logger.LogError("品种新增失败!" + ex.Message);
                result.Msg = "品种新增失败";
            }
            return result;
        }
        #endregion
    }
}
